import request from './index.js';
import store from '@/store';
import pcaa from "@/assets/js/pcaa";
import dayjs from "dayjs";
import { encrypt } from '@/utils/sms4util';
export default {
  //获取办理事务的列表
  getStepsList: (data) =>
    request({
      url: `/apply/list`,
      data: data,
      method: 'GET',
    }),
  //获取办理事务的信息
  getStepsInfo: (data) =>
    request({
      url: `/apply/queryByIdAndType`,
      data: data,
      method: 'GET',
    }),
  //获取已有证书信息
  getOldCertInfo: (data) =>
    request({
        url: `/apply/getOldCertInfo`,
        data: data,
        method: 'GET',
    }),
  //个人-申请-注册
  saveUserBusiness1: (status, data) =>
    request({
      url: `/apply/applyForRegFromPerson/${status}`,
      data: {
        ...data,
        paperCode: data.paperCode ? encrypt(data.paperCode) : undefined,
          sqrTel: data.sqrTel ? encrypt(data.sqrTel) : undefined,
          jbrTel: data.jbrTel ? encrypt(data.jbrTel) : undefined,
      },
      method: 'POST',
    }),
  //个人-申请-延续
  saveUserBusiness2: (status, data) =>
    request({
      url: `/apply/applyForExtFromPerson/${status}`,
      data: data,
      method: 'POST',
    }),
  //个人-申请-变更
  saveUserBusiness3: (status, data) =>
    request({
      url: `/apply/applyForChgFromPerson/${status}`,
      data: {
        ...data,
        paperCode: data.paperCode ? encrypt(data.paperCode) : undefined,
            sqrTel: data.sqrTel ? encrypt(data.sqrTel) : undefined,
            jbrTel: data.jbrTel ? encrypt(data.jbrTel) : undefined,
      },
      method: 'POST',
    }),
  //个人-申请-注销
  saveUserBusiness4: (status, data) =>
    request({
      url: `/apply/applyForCanFromPerson/${status}`,
      data: data,
      method: 'POST',
    }),
  //法人-申请
  saveUserBusiness5: (status, data) =>
    request({
      url: `/apply/applyForRegFromCompany/${status}`,
      data: {
          ...data,
          bussType: 5,
          creditCode: data.creditCode ? encrypt(data.creditCode) : undefined,
          unitTel: data.unitTel ? encrypt(data.unitTel) : undefined,
          fddbrZjh: data.fddbrZjh ? encrypt(data.fddbrZjh) : undefined,
      },
      method: 'POST',
    }),
  //法人-申请
  saveUserBusiness6: (status, data) =>
    request({
      url: `/apply/applyForRegFromCompany/${status}`,
      data: {
          ...data,
          bussType: 6,
            creditCode: data.creditCode ? encrypt(data.creditCode) : undefined,
            unitTel: data.unitTel ? encrypt(data.unitTel) : undefined,
            fddbrZjh: data.fddbrZjh ? encrypt(data.fddbrZjh) : undefined,
      },
      method: 'POST',
    }),
  //法人-延续
  saveUserBusiness7:(status, data) =>
    request({
        url: `/apply/applyForExtFromCompany/${status}`,
        data: {
            ...data,
            bussType: 7,
            czztdm: data.czztdm ? encrypt(data.czztdm) : undefined,
            conNum: data.conNum ? encrypt(data.conNum) : undefined,
            legRepIdCard: data.legRepIdCard ? encrypt(data.legRepIdCard) : undefined,
        },
        method: 'POST',
    }),
  //法人-变更
  saveUserBusiness8:(status, data) =>
      request({
          url: `/apply/applyForModFromCompany/${status}`,
          data: {
              ...data,
              bussType: 8,
              czztdm: data.czztdm ? encrypt(data.czztdm) : undefined,
              conNum: data.conNum ? encrypt(data.conNum) : undefined,
                legRepIdCard: data.legRepIdCard ? encrypt(data.legRepIdCard) : undefined,
          },
          method: 'POST',
      }),
  //法人-注销
  saveUserBusiness9:(status, data) =>
      request({
          url: `/apply/applyForDelFromCompany/${status}`,
          data: {...data, bussType: 9,
              czztdm: data.czztdm ? encrypt(data.czztdm) : undefined,
              conNum: data.conNum ? encrypt(data.conNum) : undefined,
              legRepIdCard: data.legRepIdCard ? encrypt(data.legRepIdCard) : undefined,},
          method: 'POST',
      }),
  // 个人-申请-三类人员-注册
    saveUserBusinessa: (status, data) =>
    request({
        url: `/apply/applyForSafeTypeReg/${status}`,
        data: {
            ...data,
            paperCode: data.paperCode ? encrypt(data.paperCode) : undefined,
            phone: data.phone ? encrypt(data.phone) : undefined,
        },
        method: 'POST',
    }),
  // 个人-申请-三类人员-延续
    saveUserBusinessb: (status, data) =>
    request({
        url: `/apply/applyForSafeTypeExt/${status}`,
        data: {
            ...data,
            paperCode: data.paperCode ? encrypt(data.paperCode) : undefined,
        },
        method: 'POST',
    }),
  // 个人-申请-三类人员-变更
    saveUserBusinessc: (status, data) =>
    request({
        url: `/apply/applyForSafeTypeChg/${status}`,
        data: {
            ...data,
            paperCode: data.paperCode ? encrypt(data.paperCode) : undefined,
        },
        method: 'POST',
    }),
  // 个人-申请-三类人员-注销
    saveUserBusinessd: (status, data) =>
    request({
        url: `/apply/applyForSafeTypeCan/${status}`,
        data: {
            ...data,
            paperCode: data.paperCode ? encrypt(data.paperCode) : undefined,
        },
        method: 'POST',
    }),
  //获取-附件规则
  getFileRule: (data) =>
    request({
      url: `/sysFileRule/list`,
      data: data,
      method: 'GET',
    }),
  //保存-附件规则
  saveFileRule: (data) =>
    request({
      url: `/sysFileRule/add`,
      data: data,
      method: 'POST',
    }),
  //删除-附件规则
  deleteFileRule: (data) =>
    request({
      url: `/sysFileRule/delete`,
      data: data,
      method: 'DELETE',
    }),
  //获取-附件列表
  getFileList: (data) =>
    request({
      url: `/sys.common/file/list`,
      data: data,
      method: 'GET',
    }),
  //申请-受理/审查
  applyApprove: (status, data) =>
    request({
      url: `/apply/approve/${status}`,
      data: data,
      method: 'POST',
    }),
    //新渝快办申请-受理/审查
    newApplyApprove: (status, data) =>
        request({
            url: `/ykbApply/approve/${status}`,
            data: data,
            method: 'POST',
        }),
  // 通过id获取证书
  getCertificateById: (data) =>
    request({
      url: `/apply/queryById`,
      data: data,
      method: 'GET',
    }),
  //用户管理列表
  getUserManagementList: (data) =>
    request({
      url: `/sys/user/list`,
      data: data,
      method: 'GET',
    }),
  //编辑-用户管理
  editUserManagement: (data) =>
    request({
        url: `/sys/user/edit`,
        data: data,
        method: 'PUT',
    }),
  //删除-用户管理
  deleteUserManagement: (data) =>
    request({
        url: `/sys/user/delete`,
        data: data,
        method: 'DELETE',
    }),
    //third user
    newThirdUser: (data) =>
    request({
        url: `/sys/user/saveThirdUser`,
        data: data,
        method: 'POST',
    }),
    editThirdUser: (data) =>
    request({
        url: `/sys/user/updateThirdUser`,
        data: data,
        method: 'PUT',
    }),
  //获取评价url接口
  getJudgeWeb: (data) =>
    request({
      url: `/apply/getJudgeWeb`,
      data: data,
      method: 'POST',
    }),
    checkUnitMajor: (data) =>
    request({
        url: `/apply/checkUnitMajor`,
        data: data,
        method: 'GET',
    }),
    checkUnitMajorApply: (data) =>
    request({
        url: `/apply/checkUnitMajorApply`,
        data: data,
        method: 'GET',
    }),
    // 获取用户对应业务类型
    getUserBussType: (userType) => {
        let bussTypes = []
        // 3,5 二级造价
        if (userType.includes('5')
            || userType.includes('3')) {
            bussTypes.push(['1','2','3','4'])
        }
        // 4,6 乙级
        if (userType.includes('6')
            || userType.includes('4')) {
            bussTypes.push(['5','6','7','8','9'])
        }
        // 8,9 三类人员
        if (userType.includes('8')
            || userType.includes('9')) {
            bussTypes.push(['a','b','c','d'])
        }
        return bussTypes?.join(',')
    },
    // 获取用户对应业务状态
    getUserBussStatus: (userType) => {
        let status = []
        if (userType.includes('3')
            || userType.includes('4')
            || userType.includes('8')) {
            status.push(1)
        }
        if (userType.includes('5')
            || userType.includes('6')
            || userType.includes('9')) {
            status.push(3)
        }
        return status?.join(',')
    },
    // 是否是企业业务类型
    isCompanyBussType: (bussType) => {
      const bussTypeStr = `${bussType}`;
      return ['6','7','8','9'].includes(bussTypeStr)
    },
    // 是否是个人业务类型
    isPersonBussType: (bussType) => {
        const bussTypeStr = `${bussType}`;
        return ['1','2','3','4','a','b','c','d'].includes(bussTypeStr)
    },
    // 是否是注册业务类型
    isRegBussType: (bussType) => {
        const bussTypeStr = `${bussType}`;
        return ['1','5','6','a'].includes(bussTypeStr)
    },
    // 获取可申请专业
    getApplyMajors: (dicts, ext=false, process=false) => {
      let all = dicts;
      if (process) {
          return all;
      }
      const qua = store.state.user.userQua;
      let has = [];
      if (qua) {
          if (qua.ytCode) {
              has.push('1');
          }
          if (qua.hltCode) {
              has.push('2');
          }
          if (qua.jsjgCode) {
              has.push('3');
          }
          if (qua.jxdqCode) {
              has.push('4');
          }
          if (qua.lcCode) {
              has.push('5');
          }
      }
      if (!ext) {
          all = all.filter(item => !has.includes(item.value));
      } else {
          all = all.filter(item => has.includes(item.value));
      }
      return all;
    },
    //转换行政区划名称
    getAddr: (code, second) => {
      const all = pcaa['86'];
      const cqAddr = all['500000'];
      const cq = pcaa['500000'];
      if (second) {
          const secondAddr = cq[second];
          const secondCodes = pcaa[second];
          const addr = secondCodes[code];
          return `${cqAddr}${secondAddr}${addr}`?.replace('市辖区', '');
      }
      return '';
    },
    //企业是否可以更改信息
    canEdit: () => {
      const qua = store.state.user.userQua
      return !(qua?.hltCode || qua?.jsjgCode || qua?.jxdqCode || qua?.lcCode || qua?.ytCode)
    },
    // 三类人员是否可以注册
    canReg: (type) => {
      const {userQua, mwrCerts} = store.state.user;
      let mwrCert;
      mwrCerts?.forEach(item => {
          const {REG_INFO} = item;
            if (REG_INFO) {
                const {SAFE_TYPE_CODE} = REG_INFO;
                if (SAFE_TYPE_CODE === type) {
                    mwrCert = item;
                }
            }
        });
      if (type == 'A' && !(userQua.mainCertCode || mwrCert)) {
        return true;
      }
      if (type == 'B' && !(userQua.projCertCode || mwrCert)) {
        return true;
      }
      if (type == 'C' && !(userQua.classifyCertCode || mwrCert)) {
        return true;
      }
      return false;
    },
    // 获取水利部注册信息
    getCertInfoFromMwr: (type) => {
      const { mwrCerts } = store.state.user;
      let cert;
      mwrCerts?.forEach(item => {
          const {REG_INFO, ISSU_DATE,
              VAL_PER_FROM, VAL_PER_TO, EVO_INFO,
              // HOLDER_INFO,
          } = item;
          if (REG_INFO) {
              const {SAFE_TYPE_CODE} = REG_INFO;
              if (SAFE_TYPE_CODE === type) {
                  cert = {
                      certNumCode: item.CERT_NUM_CODE,
                      duty: REG_INFO.DUTY,
                      issuDate: dayjs(ISSU_DATE).format('YYYY-MM-DD'),
                      entName: REG_INFO.ENT_NAME,
                      entLevel: REG_INFO.ENT_LEVEL,
                      effStartTime: dayjs(VAL_PER_FROM).format('YYYY-MM-DD'),
                      effEndTime: dayjs(VAL_PER_TO).format('YYYY-MM-DD'),
                      firstIssuDate: dayjs(EVO_INFO.FIR_ISSU_DATE).format('YYYY-MM-DD'),
                  };
              }
          }
      });
      return cert;
    },
    // 忽略空值
    ignoreEmpty: (obj) => {
        const newObj = {};
        Object.keys(obj).forEach(key => {
            if (obj[key]) {
                newObj[key] = obj[key];
            }
        });
        return newObj;
    },
    // 获取证书类型
    getCertType: (bussType) => {
      if (['1','2','3','4'].includes(bussType)) {
          return '1';
      }
        if (['5','6','7','8','9'].includes(bussType)) {
            return '2';
        }
        if (['a','b','c','d'].includes(bussType)) {
            return '3';
        }
        return '1';
    },
    // 查询水利部三类人员电子证照
    getCertsFromMwr: (data) =>
        request({
            url: `/mwr/queryByHolderCode`,
            data: data,
            method: 'GET',
        }),
    getMvCerts: (data) =>
        request({
            url: `/mwr/queryMv`,
            data: data,
            method: 'GET',
        }),
    // 下载水利部三类人员电子证照
    downloadCertFromMwr: (data) =>
        request({
            url: `/mwr/downloadCert`,
            data: data,
            method: 'GET',
        })
};
